package com.ipet.app.ui.activity;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ipet.app.R;
import com.ipet.app.adapter.ActivityReplyAdapter;
import com.ipet.app.api.BaseCallBack;
import com.ipet.app.api.RequestManager;
import com.ipet.app.bean.HotPost;
import com.ipet.app.bean.ReplyInfo;
import com.ipet.app.bean.apiRequestBean.PostDetailRequest;
import com.ipet.app.bean.apiRequestBean.ReplyPostRequest;
import com.ipet.app.bean.apiResponseBean.BaseResponse;
import com.ipet.app.bean.apiResponseBean.PostDetailResponse;
import com.ipet.app.ui.base.SwipeBackActivity;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.List;

/**
 * Created by jindongping on 16/3/23.
 * HotPost帖子数据
 * from 从哪个视图跳转过来
 */
public class PostDetailActivity extends SwipeBackActivity {

    private CoordinatorLayout coordinatorLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);
        initToolbar();
        initData();
    }

    private void initData() {
        HotPost hotPost = (HotPost) getIntent().getSerializableExtra("hotPost");
        if (hotPost == null) {
            finish();
            return;
        }
        initView(hotPost);
    }

    private void initView(final HotPost data) {
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        initRecycleView(data);
        initSendView(data);
    }

    int page = 1, limit = 15;
    PostDetailRequest request;
    ActivityReplyAdapter adapter;

    private void initRecycleView(HotPost data) {
        final EasyRecyclerView recyclerView = (EasyRecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(PostDetailActivity.this));
        recyclerView.getSwipeToRefresh().setColorSchemeResources(R.color.pink_500);
        adapter = new ActivityReplyAdapter(PostDetailActivity.this, data);
        //增加一个空的数据 用于展示 帖子
        adapter.add(new ReplyInfo());
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setError(R.layout.view_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.resumeMore();
            }
        });
        recyclerView.setAdapter(adapter);

        request = new PostDetailRequest(data.getActive_id(), page + "", limit + "");
        //设置下拉刷新监听
        recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                request.setPage(page + "");
                //下拉加载数据
                RequestManager.getApi().getPostDetail(request).enqueue(new BaseCallBack<PostDetailResponse>(coordinatorLayout, recyclerView.getSwipeToRefresh()) {
                    @Override
                    public void onRequestSuccess(PostDetailResponse response) {
                        //下拉刷新 恢复page
                        adapter.clear();
                        //增加一个空的数据 用于展示 帖子
                        adapter.add(new ReplyInfo());
                        adapter.addAll(response.getReplyInfo());
                    }
                });
            }
        });

        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                recyclerView.setRefreshing(true);
                RequestManager.getApi().getPostDetail(request).enqueue(new BaseCallBack<PostDetailResponse>(coordinatorLayout) {
                    @Override
                    public void onRequestSuccess(PostDetailResponse response) {
                        List<ReplyInfo> replyInfos = response.getReplyInfo();
                        adapter.addAll(replyInfos);
                    }
                });
            }
        });


        adapter.setMore(R.layout.view_more, new RecyclerArrayAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                request.setPage(++page + "");
                RequestManager.getApi().getPostDetail(request).enqueue(new BaseCallBack<PostDetailResponse>(coordinatorLayout, recyclerView.getSwipeToRefresh()) {
                    @Override
                    public void onRequestSuccess(PostDetailResponse response) {
                        adapter.addAll(response.getReplyInfo());
                    }
                });

            }
        });
    }

    /**
     * 初始化发送界面
     */
    private void initSendView(final HotPost data) {
        Button button = (Button) findViewById(R.id.send_reply);
        final EditText editText = (EditText) findViewById(R.id.et_content);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString().trim();
                if (content.length() < 1) {
                    snackShow(coordinatorLayout, "评论不能为空");
                    return;
                }
                final EasyRecyclerView recyclerView = (EasyRecyclerView) findViewById(R.id.recycler_view);
                RequestManager.getApi().replyPost(new ReplyPostRequest(data.getActive_id(), content, data.getUser_id())).enqueue(new BaseCallBack<BaseResponse>(coordinatorLayout,recyclerView.getSwipeToRefresh()) {
                    @Override
                    public void onRequestSuccess(BaseResponse response) {
                        editText.setText("");
                        snackShow(coordinatorLayout, "评论成功");
                        //自动刷新评论
                        recyclerView.post(new Runnable() {
                            @Override
                            public void run() {
                                recyclerView.setRefreshing(true);
                                page = 1;
                                request.setPage(page + "");
                                RequestManager.getApi().getPostDetail(request).enqueue(new BaseCallBack<PostDetailResponse>(coordinatorLayout) {
                                    @Override
                                    public void onRequestSuccess(PostDetailResponse response) {
                                        List<ReplyInfo> replyInfos = response.getReplyInfo();
                                        adapter.clear();
                                        //增加一个空的数据 用于展示 帖子
                                        adapter.add(new ReplyInfo());
                                        adapter.addAll(replyInfos);
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("帖子详情");
        toolbar.setNavigationIcon(R.drawable.btn_back);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
